home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / xlib03.zip / DEMO4.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  5KB  |  162 lines

  1. /*************************************************************************
  2.  
  3. DEMO 4
  4.  
  5. Demonstrates RLE encoding to file QUICK and DIRTY
  6. **************************************************************************/
  7.  
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <ctype.h>
  14. #include <alloc.h>
  15. #include <dos.h>
  16. #include "xlib.h"
  17. #include "xpbitmap.h"
  18. #include "xbmtools.h"
  19. #include  "xtext.h"
  20. #include  "xrect.h"
  21. #include  "xrletool.h"
  22. #include  "xfileio.h"
  23. /* Macro to make pointer parameters model independent */
  24. #define FARPTR(x) (MK_FP(FP_SEG(x),FP_OFF(x)))
  25.  
  26. char turtle[] = {
  27.     20,30,
  28. /*  |      .       |       .      |       .      |      .       */
  29.     8,14, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30.     8, 2,14, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31.     8, 2, 2, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32.     8, 0, 2,14, 0, 0,14,14,14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33.     8, 0, 2, 5, 0, 4, 4, 4, 4,14,14, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34.     8, 0, 2, 2, 4, 4, 0, 4, 4, 0, 4,14, 0, 0, 0, 0, 0, 0, 0, 0,
  35.     0, 0, 2, 4, 4, 4, 0, 4, 4, 0, 4, 4,14, 0, 0, 0, 0, 0, 0, 0,
  36.     0, 0, 2, 4, 4, 4, 0, 0, 0, 0, 4, 4,14, 0, 0, 0, 0, 0, 0, 0,
  37.     0, 0, 0, 0, 4, 0, 4, 4, 4, 0, 0, 0, 4, 0, 0, 2, 2, 0, 0, 0,
  38.     0, 0, 0, 4, 0, 4, 4, 4, 0, 4, 4, 4, 0,14, 0, 0, 2, 2, 0, 0,
  39.     0, 0, 0, 4, 4, 0, 0, 0, 4, 4, 0, 4, 0, 4, 2, 2, 2, 2, 0, 0,
  40.     0, 0, 0, 4, 4, 4, 0, 4, 4, 0, 4, 4, 0, 4,14, 2, 2, 2, 0, 0,
  41.     0, 0, 0, 4, 4, 4, 0, 0, 0, 4, 4, 0, 4, 0, 0, 2, 2, 0, 0, 0,
  42.     0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4,14, 2, 0, 0, 0, 0,
  43.     0, 0, 2, 2, 0, 4, 4, 4, 4, 0, 0, 4, 4, 4, 4, 0, 0, 0, 0, 0,
  44.     2, 2, 2, 2, 4, 0, 0, 4, 4, 0, 4, 4, 0, 4,14, 0, 0, 0, 0, 0,
  45.     0, 2, 2, 0, 4, 4, 4, 0, 0, 4, 4, 0, 4, 0, 2, 0, 0, 0, 0, 0,
  46.     0, 0, 0, 0, 0, 4, 4, 0, 4, 4, 4, 0, 4, 4, 2,14, 0, 0, 0, 0,
  47.     0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 4, 0, 2, 2,14,14, 0, 0,
  48.     0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 4, 2, 2, 2, 2, 2,14, 0,
  49.     0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 5,14, 0,
  50.     0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2, 0, 0, 2, 2, 2, 0, 2,14,
  51.     0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2,14,
  52.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2,
  53.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, 0,
  54.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0,
  55.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0,
  56.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0,
  57.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
  58.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  59.     };
  60.  
  61.  
  62. char far *pbm;
  63. char far *newfnt;
  64. int  far *buffsize;
  65.  
  66. void error(char * string){
  67.   x_text_mode();
  68.   printf("%s\n",string);
  69.   exit(0);
  70. }
  71.  
  72. void load_user_fonts(){
  73.   FILE *f;
  74.   int i; char c;
  75.   f=fopen("6x8b.fnt","rb");
  76.   newfnt = farmalloc(256*8+4);
  77.   for (i=0;i<256*8+4;i++){
  78.     fread(&c,1,1,f);
  79.     *(newfnt+i)=c;
  80.   }
  81.   fclose(f);
  82.   x_text_init();
  83.   x_register_userfont(newfnt);
  84.   x_set_font(2);
  85. }
  86.  
  87. void main(){
  88.     int i,j,handle,size,comp_size;
  89.     char far * tempbuff,far * diskbuff;
  90.  
  91.     pbm = farmalloc(20 * 30 + 2);
  92.     x_bm_to_pbm( MK_FP(FP_SEG(turtle),FP_OFF(turtle)), pbm);
  93.  
  94.     x_set_mode(X_MODE_360x240,360);
  95.     load_user_fonts();
  96.  
  97.     x_bgprintf(0,0,0,14,0,
  98.     "This is a demo of RLE compression of 360x240 video screen to ");
  99.     x_bgprintf(0,8,0,14,0,
  100.     "disk. Uncompressed, this screen requires a file of size ");
  101.     x_bgprintf(0,16,0,14,0,"86400 (360x240) bytes.");
  102.     x_bgprintf(0,48,0,14,0,
  103.     "Press a key to draw the image and save it to screen:");
  104.  
  105.     getch();
  106.     x_rect_fill(0,0,ScrnPhysicalPixelWidth,ScrnPhysicalHeight,0,0);
  107.  
  108.     for (j=0;j<(ScrnPhysicalHeight/30);j++)
  109.       for (i=0;i<(ScrnPhysicalPixelWidth/20);i++)
  110.     x_put_masked_pbm(i*20, j*30, 0, pbm);
  111.  
  112.     x_set_start_addr(0,240);
  113.  
  114.     x_bgprintf(0,0,240*90,14,0,
  115.     "Now compressing and writing to file 'screen.dat'.");
  116.     x_bgprintf(0,8,240*90,14,0,"Please wait... ");
  117.  
  118.  
  119.     if((handle=f_open("screen.dat",F_WRONLY))==FILE_ERR)
  120.       error("Unable to open 'screen.dat' for writing.");
  121.     size=0;
  122.     tempbuff=farmalloc(90*240);
  123.     diskbuff=farmalloc(90*240);
  124.     buffsize=(int far *)farmalloc(2);
  125.  
  126.     for (i=0;i<4;i++){
  127.       x_select_default_plane(i);
  128.       if (!(j=x_buff_RLEncode(MK_FP(0xA000,0),tempbuff,90*240)))
  129.         error("Error buff compressing");
  130.       *buffsize=j;
  131.       f_write(handle,(char far *)buffsize,2);
  132.       f_write(handle,tempbuff,j);
  133.       size+=j;
  134.     }
  135.     comp_size=size;
  136.     f_close(handle);
  137.  
  138.     x_bgprintf(0,8,240*90,14,0,"Done. Press a key to restore.");
  139.     getch();
  140.  
  141.     x_rect_fill(0,0,ScrnPhysicalPixelWidth,ScrnPhysicalHeight,0,0);
  142.     x_set_start_addr(0,0);
  143.  
  144.     if((handle=f_open("screen.dat",F_RDONLY))==FILE_ERR)
  145.       error("Unable to open 'screen.dat' for reading.");
  146.  
  147.     size=0;
  148.     for (i=0;i<4;i++){
  149.       x_select_default_plane(i);
  150.         f_read(handle,(char far *)buffsize,2);
  151.         f_read(handle,diskbuff,*buffsize);
  152.         j=x_buff_RLDecode(diskbuff,MK_FP(0xA000,0));
  153.         size+=j;
  154.     }
  155.     f_close(handle);
  156.  
  157.     getch();
  158.     x_text_mode();
  159.     printf("screen size = 86400  compressed = %u",comp_size);
  160. }
  161.  
  162.